package com.orientation.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.orientation.commom.Result;
import com.orientation.dto.MajorInfoDto;
import com.orientation.entity.Major;
import com.orientation.service.MajorService;
import com.orientation.vo.MajorClassStudentVo;
import com.orientation.vo.MajorVo;
import com.orientation.vo.ProgressLengthVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 专业 前端控制器
 * </p>
 *
 * @author xuandian
 * @since 2022-3-9
 */
@Api(tags = "专业信息")
@RestController
@RequestMapping("/major")
public class MajorController {
    @Resource
    private MajorService majorService;

    @ApiOperation("添加")
    @PostMapping
    public ResponseEntity<Result> saveMajor(@RequestBody Major major) {
        majorService.saveMajor(major);
        return ResponseEntity.ok(new Result());
    }

    // public boolean save(@RequestBody Major major) {
//        return majorService.save(major);
//    }

    @ApiOperation("修改")
    @PutMapping
    public boolean update(@RequestBody Major major) {
        return majorService.updateById(major);
    }

    @ApiOperation("id删除")
    @DeleteMapping("/{id}")
    public boolean delete(@PathVariable String id) {
        return majorService.removeById(id);
    }

    @ApiOperation("id查询")
    @GetMapping("/{id}")
    public Major findById(@PathVariable String id) {
        return majorService.getById(id);
    }

    @ApiOperation("列表查询")
    @GetMapping("list")
    public List<Major> findAll() {
        return majorService.list();
    }

    //    @ApiOperation("分页查询显示 废弃不用")
//    @GetMapping("/page")
//    public IPage<Major> findPage(@RequestParam(required = false, defaultValue = "") String name,
//                                 @RequestParam(required = false, defaultValue = "1") Integer pageNum,
//                                 @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
//        return majorService.page(new Page<>(pageNum, pageSize), Wrappers.<Major>lambdaQuery().like(StringUtils.isNotBlank(name), Major::getMajorName, name));
//    }
    @ApiOperation("分页查询")
    @GetMapping("/page")
    public ResponseEntity<Result> findPage(@RequestParam(required = false, defaultValue = "") String name,
                                           @RequestParam(required = false, defaultValue = "") String collegeName,
                                           @RequestParam(required = false, defaultValue = "") String endDate,
                                           @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                           @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        // return collegeService.page(new Page<>(pageNum, pageSize), Wrappers.<College>lambdaQuery().like(StringUtils.isNotBlank(name), College::getCollegeName, name));
        IPage<MajorVo> majorVoIPage = majorService.queryMajor(pageNum, pageSize, name, collegeName, endDate);
        return ResponseEntity.ok(new Result(majorVoIPage));

    }

    @ApiOperation("分页查询已经启用状态")
    @GetMapping("/queryStateMajor")
    public ResponseEntity<Result> queryStateMajor(@RequestParam(required = false, defaultValue = "") String majorName,
                                                  @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                                  @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        IPage<MajorVo> majorVoIPage = majorService.queryStateMajor(pageNum, pageSize, majorName);
        return ResponseEntity.ok(new Result(majorVoIPage));

    }

    /**
     * 修改启用状态
     */
    @ApiOperation("修改启用状态")
    @PostMapping("/updateStates")
    public ResponseEntity<Result> insertMajor(@RequestBody Major major) {
        majorService.updateStates(major);
        return ResponseEntity.ok(new Result());
    }

    /**
     * 添加专业信息
     */
    @ApiOperation("添加专业信息/废弃不用")
    @PostMapping("/insertMajor")
    public void insertMajor(@RequestBody MajorInfoDto majorInfoDto) {
        majorService.insertMajor(majorInfoDto);
    }

    /**
     * 专业下拉筛选框
     *
     * @param collegeId
     * @return
     */
    @ApiOperation("专业下拉筛选框")
    @GetMapping("/queryList")
    public ResponseEntity<Result> queryMajorList(@RequestParam(value = "collegeId", required = false) String collegeId,
                                                 @RequestParam(value = "type", required = false, defaultValue = "0") Integer type) {
        List<Major> majorList = majorService.queryMaj(collegeId, type);
        return ResponseEntity.ok(new Result(majorList));
    }

    @ApiOperation("专业下拉筛选框")
    @GetMapping("/map")
    public Map<String, Major> findAllMap(String collegeId) {
        return majorService.list(Wrappers.<Major>lambdaQuery().eq(StringUtils.isNotBlank(collegeId), Major::getCollegeId, collegeId)).stream().collect(Collectors.toMap(Major::getSid, r -> r));
    }

    @ApiOperation("查询院系下所有专业")
    @GetMapping("/byCollegeId")
    public List<Major> findAllByCollegeId(String collegeId) {
        return majorService.list(Wrappers.<Major>lambdaQuery().eq(StringUtils.isNotBlank(collegeId), Major::getCollegeId, collegeId));
    }

    @ApiOperation("修改专业状态 废弃不用")
    @PostMapping("/updateState")
    public boolean updateState(@RequestParam(value = "majorId") String majorId, @RequestParam(value = "state") Integer state) {
        return majorService.updateState(majorId, state);
    }

    @ApiOperation("查询专业班级写学生 废弃不用")
    @GetMapping("/queryMajorName")
    public ResponseEntity<Result> queryMajorName(@RequestParam(value = "majorName", required = false) String majorName) {
        List<MajorClassStudentVo> majorClassStudentVos = majorService.queryMajorName(majorName);
        return ResponseEntity.ok(new Result(majorClassStudentVos));
    }

    @ApiOperation("分页查询专业班级下学生")
    @GetMapping("/queryPageMajorName")
    public ResponseEntity<Result> queryPageMajorName(@RequestParam(required = false, defaultValue = "") String majorName,
                                                     @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                                     @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        Page<MajorClassStudentVo> classStudentVoPage = majorService.queryPageMajorName(pageNum, pageSize, majorName);
        return ResponseEntity.ok(new Result(classStudentVoPage));

    }

    @ApiOperation("各专业报道进度条")
    @GetMapping("/progressLengthVos")
    public ResponseEntity<Result> progressLengthVos(@RequestParam(value = "collegeId", required = false) String collegeId) {
        List<ProgressLengthVo> lengthVos = majorService.progressLengthVos(collegeId);
        return ResponseEntity.ok(Result.success(lengthVos));

    }

    @ApiOperation("各专业床位进度条")
    @GetMapping("/getBedProgressLengthVos")
    public ResponseEntity<Result> getBedProgressLengthVos(@RequestParam(value = "collegeId", required = false) String collegeId) {
        List<ProgressLengthVo> lengthVos = majorService.getBedProgressLengthVos(collegeId);
        return ResponseEntity.ok(Result.success(lengthVos));

    }

    @ApiOperation("各专业物资领取进度条")
    @GetMapping("/getGoodsProgressLengthVos")
    public ResponseEntity<Result> getGoodsProgressLengthVos(@RequestParam(value = "collegeId", required = false) String collegeId) {
        List<ProgressLengthVo> lengthVos = majorService.getGoodsProgressLengthVos(collegeId);
        return ResponseEntity.ok(Result.success(lengthVos));
    }

}

